<template>
  <div>
    <el-card style="margin:20px 0">
      <div class="sale">
        <!-- @tab-click="handleClick" -->
        <!-- 左侧区域 -->
        <div class="left">
          <el-tabs v-model="activeName" class="tabs">
            <el-tab-pane label="销售额" name="sale">

            </el-tab-pane>
            <el-tab-pane label="访问量" name="visit">

            </el-tab-pane>
          </el-tabs>
        </div>

        <!-- 右侧区域 -->
        <div class="right">
          <span>今日</span>
          <span>本周</span>
          <span>本月</span>
          <span>本年</span>
          <!-- 时间选择器 -->
          <!--  v-model="value1" -->
          <el-date-picker style="margin:0 20px" type="daterange" range-separator="-" start-placeholder="开始日期"
            end-placeholder="结束日期" size="mini" v-model="date" value-format="yyyy-MM-dd">
          </el-date-picker>
        </div>
      </div>
      <el-row>
        <el-col :span="16">
          <div ref="dom" class="charts">
          </div>
        </el-col>
        <el-col :span="8">1122</el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { title } from '@/settings';
import * as echarts from 'echarts';
import { mapState } from 'vuex';
export default {
  data() {
    return {
      activeName: 'sale',
      date: [],
      Mycharts: null
    }
  },
  mounted() {
    this.Mycharts = echarts.init(this.$refs.dom)
    this.Mycharts.setOption({
      //标题数据
      title: {
        text: this.title + '趋势'
      },
      xAxis: {
        type: 'category',
        data: []
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: [],
          type: 'bar',
          showBackground: true,
          backgroundStyle: {
            color: 'rgba(180, 180, 180, 0.2)'
          }
        }
      ]
    })
  },
  computed: {
    title() {
      return this.activeName == 'sale' ? '销售额' : '访问量'
    },
    ...mapState({
      listState: state => {
        return state.home.list
      }
    })
  },
  watch: {
    title() {
      this.Mycharts.setOption({
        //标题数据
        title: {
          text: this.title + '趋势'
        },
        xAxis: {
          data:
            this.title == "销售额"
              ? this.listState.orderFullYearAxis
              : this.listState.userFullYearAxis,
        },
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data:
              this.title == "销售额"
                ? this.listState.orderFullYear
                : this.listState.userFullYear,
            color: "yellowgreen",
          },
        ],
      })
    },
    listState() {
      this.Mycharts.setOption({
        title: {
          text: this.title + "趋势",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: this.listState.orderFullYearAxis,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data: this.listState.orderFullYear,
            color: "yellowgreen",
          },
        ],
      });
    }
  }

}
</script>

<style scoped>
.sale {
  position: relative;
  display: flex;
  justify-content: space-between;
}

.right span {
  font-size: 14px;
  margin: 0 10px;
  margin-top: 5px;

}

.right {
  position: absolute;
  right: 0;
  height: 30px;
}

.left {
  width: 100%;
}

.charts {
  width: 100%;
  height: 300px;
}
</style>
